using System;
using System.Data;
using System.Windows.Forms;
using TUnit.Data;
using TUnit.Class;
namespace viclip
{
    ///<summary>
    ///Date init:  
    ///Date modify:
    ///By:
    ///
    ///</summary>
    public class CPayCards : Class
    {
        #region Declares
        int _cardid = -1;
        string _cardno = "";
        string _serino = "";
        int _price = -1;
        string _status = "";
        string _telco = "";
        string _session_id = "";
        #endregion

        #region Constructors
        public CPayCards()
        {
        }
        public CPayCards(CLogin Login)
        {
            base.Login = Login;
        }
        public CPayCards(CLogin Login, object ObjId)
        {
            base.Login = Login;
            using (AccDBPayCards db = new AccDBPayCards())
            {
                this.Populate((CPayCards)db.GetClass(Login, ObjId));
            }
        }
        public CPayCards(CLogin Login, IProvider p, System.Data.DataRow dr)
        {
            base.Login = Login;
            this.Populate(p, dr);
        }
        public CPayCards(CLogin Login, IProvider p, object ObjId)
        {
            base.Login = Login;
            using (AccDBPayCards db = new AccDBPayCards())
            {
                this.Populate((CPayCards)db.GetClass(Login, p, ObjId));
            }
        }
        #endregion

        #region Methods
        public void Populate(IProvider p, DataRow dr)
        {
            if (dr != null)
            {
                try
                {
                    if (dr.Table.Columns.IndexOf("cardid") >= 0)
                    {
                        this._cardid = (dr["cardid"] != DBNull.Value ? Convert.ToInt32(dr["cardid"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblPayCards ADD cardid [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("cardno") >= 0)
                    {
                        this._cardno = (dr["cardno"] != DBNull.Value ? Convert.ToString(dr["cardno"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblPayCards ADD cardno [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("serino") >= 0)
                    {
                        this._serino = (dr["serino"] != DBNull.Value ? Convert.ToString(dr["serino"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblPayCards ADD serino [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("price") >= 0)
                    {
                        this._price = (dr["price"] != DBNull.Value ? Convert.ToInt32(dr["price"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblPayCards ADD price [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("status") >= 0)
                    {
                        this._status = (dr["status"] != DBNull.Value ? Convert.ToString(dr["status"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblPayCards ADD status [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("telco") >= 0)
                    {
                        this._telco = (dr["telco"] != DBNull.Value ? Convert.ToString(dr["telco"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblPayCards ADD telco [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("session_id") >= 0)
                    {
                        this._session_id = (dr["session_id"] != DBNull.Value ? Convert.ToString(dr["session_id"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblPayCards ADD session_id [nvarchar] (50) NULL");
                    //}
                }
                catch { }
            }
        }
        public void Populate(CPayCards Obj)
        {
            if (Obj != null)
            {
                try
                {
                    this._cardid = Obj.cardid;
                    this._cardno = Obj.cardno;
                    this._serino = Obj.serino;
                    this._price = Obj.price;
                    this._status = Obj.status;
                    this._telco = Obj.telco;
                    this._session_id = Obj.session_id;
                }
                catch { }
            }
        }
        public override bool Save()
        {
            using (IProvider p = new OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBPayCards db = new AccDBPayCards())
                    {
                        if (this.EventSaving != null)
                            this.EventSaving(this);
                        if (db.Save(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaved != null)
                                this.EventSaved(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaveError != null)
                                this.EventSaveError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
        public override bool Save(IProvider p)
        {
            using (AccDBPayCards db = new AccDBPayCards())
            {
                if (db.Save(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        public override bool Delete()
        {
            using (IProvider p = new OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBPayCards db = new AccDBPayCards())
                    {
                        if (this.EventDeleting != null)
                            this.EventDeleting(this);
                        if (db.Delete(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleted != null)
                                this.EventDeleted(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleteError != null)
                                this.EventDeleteError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
        public override bool Delete(IProvider p)
        {
            using (AccDBPayCards db = new AccDBPayCards())
            {
                if (db.Delete(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        #endregion

        #region Events
        public delegate void OnEventSaveError(CPayCards obj);
        public event OnEventSaveError EventSaveError;
        public delegate void OnEventSaving(CPayCards PayCards);
        public event OnEventSaving EventSaving;
        public delegate void OnEventSaved(CPayCards PayCards);
        public event OnEventSaved EventSaved;
        public delegate void OnEventDeleting(CPayCards PayCards);
        public event OnEventSaving EventDeleting;
        public delegate void OnEventDeleted(CPayCards PayCards);
        public event OnEventDeleted EventDeleted;
        public delegate void OnEventDeleteError(CPayCards obj);
        public event OnEventDeleteError EventDeleteError;
        #endregion

        #region Properties
        public int cardid
        {
            get { return this._cardid; }
            set
            {
                if (this._cardid != value)
                {
                    this._cardid = value;
                }
            }
        }
        public string cardno
        {
            get { return this._cardno; }
            set
            {
                if (this._cardno != value)
                {
                    this._cardno = value;
                }
            }
        }
        public string serino
        {
            get { return this._serino; }
            set
            {
                if (this._serino != value)
                {
                    this._serino = value;
                }
            }
        }
        public int price
        {
            get { return this._price; }
            set
            {
                if (this._price != value)
                {
                    this._price = value;
                }
            }
        }
        public string status
        {
            get { return this._status; }
            set
            {
                if (this._status != value)
                {
                    this._status = value;
                }
            }
        }
        public string telco
        {
            get { return this._telco; }
            set
            {
                if (this._telco != value)
                {
                    this._telco = value;
                }
            }
        }
        public string session_id
        {
            get { return this._session_id; }
            set
            {
                if (this._session_id != value)
                {
                    this._session_id = value;
                }
            }
        }
        #endregion
    }

    #region Class AccDB
    public class AccDBPayCards : AccDB
    {
        /// <summary>
        /// 
        /// </summary>
        public AccDBPayCards()
        {
        }
        /// <summary>
        ///Phuong thuc dung dien du lieu len Combo 
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="cmb"></param>
        /// <param name="extName"></param>
        public override void FillComboBox(CLogin Login, ComboBox cmb, string extName)
        {
            IProvider p = new OleDb(Login.Connectionstring);
            if (p.Open())
            {
                string sql = "SELECT PayCardsId, PayCardsName";
                sql += " FROM  tblPayCards";
                sql += " WHERE (PayCardsId >= 0)";
                sql += " ORDER BY PayCardsName";
                DataTable table = p.OpenTable(sql, "tblPayCards");
                if (table != null)
                {
                    if (extName != "")
                    {
                        System.Data.DataRow row = table.NewRow();
                        row["PayCardsName"] = extName;
                        row["PayCardsId"] = "-1";
                        table.Rows.Add(row);
                    }
                    cmb.DataSource = table;
                    cmb.DisplayMember = "PayCardsName";
                    cmb.ValueMember = "PayCardsId";
                    p.Close();
                }
            }
        }
        /// <summary>
        ///Doc doi tuong tu CSDL theo ID chi dinh
        /// </summary>
        /// <param name="p"></param>
        /// <param name="ObjId"></param>
        /// <returns></returns>
        public override Class GetClass(CLogin Login, IProvider p, object ObjId)
        {
            try
            {
                string sql = "SELECT * FROM tblPayCards WHERE (PayCardsId = " + ObjId + ")";
                DataTable table = p.OpenTable(sql, "tblPayCards");
                if ((table != null) && (table.Rows.Count > 0))
                {
                    return new CPayCards(Login, p, table.Rows[0]);
                }
            }
            catch
            {
            }
            return null;
        }
        /// <summary>
        /// Luu doi tuong vao CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Save(IProvider p, Class obj)
        {
            CPayCards PayCards = (CPayCards)obj;
            string sql = "SELECT * FROM tblPayCards";
            sql += " WHERE cardid =" + PayCards.cardid + "";
            //sql +=" AND CompanyId =" + PayCards.CompanyId + "";
            DataTable dt = p.OpenTable(sql, "tblPayCards");
            if (dt != null)
            {
                DataRow row = null;
                if (dt.Rows.Count == 1 && PayCards.cardid != -1)
                {
                    row = dt.Rows[0];
                }
                else
                {
                    PayCards.cardid = Convert.ToInt32(p.InitIDKey("cardid", "tblPayCards", null));
                    row = dt.NewRow();
                    dt.Rows.Add(row);
                }
                row["cardid"] = PayCards.cardid;
                row["cardno"] = PayCards.cardno;
                row["serino"] = PayCards.serino;
                row["price"] = PayCards.price;
                row["status"] = PayCards.status;
                row["telco"] = PayCards.telco;
                row["session_id"] = PayCards.session_id;
                if (!p.UpdateTable("tblPayCards", dt))
                {
                    return false;
                }
                return true;
            }
            return false;
        }
        /// <summary>
        ///Xoa doi tuong khoi CSDL 
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Delete(IProvider p, Class obj)
        {
            CPayCards PayCards = (CPayCards)obj;
            string sql = "DELETE FROM tblPayCards";
            sql += " WHERE cardid =" + PayCards.cardid + "";
            //sql +=" AND CompanyId =" + PayCards.CompanyId + "";
            return p.ExecuteNonQuery(sql);
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong tren CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(CLogin Login, IProvider p, CCollection Collection)
        {
            string sql = "SELECT * FROM tblPayCards ORDER BY PayCardsName";
            DataTable table = p.OpenTable(sql, "tblPayCards");
            if (table != null)
            {
                foreach (DataRow row in table.Rows)
                {
                    CPayCards o = new CPayCards(Login, p, row);
                    Collection.Add(o);
                }
            }
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong con cua doi obj neu co (danh cho loai du lieu quan he 2 bang cha con)
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(IProvider p, Class obj, CCollection Collection)
        {
        }
    }
    #endregion
}
